package com.huluobo.a2201mvvm_imdemo.addGroup;

import androidx.lifecycle.MutableLiveData;

import com.huluobo.a2201mvvm_imdemo.mvvm.BaseRepo;
import com.hyphenate.EMCallBack;
import com.hyphenate.EMValueCallBack;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMCursorResult;
import com.hyphenate.chat.EMGroup;
import com.hyphenate.chat.EMGroupInfo;
import com.hyphenate.chat.EMGroupOptions;
import com.hyphenate.exceptions.HyphenateException;

import java.util.List;

/**
 * Created by LC on 2024/3/28.
 * 创建群的参数可以从emClient中groupManager中createGroup点进去,拿到参数
 */
public class AddGroupRepo extends BaseRepo {
    public void createCroup(String groupName,//群的名字
                            String desc,//群的描述
                            String[] allMembers,//创建群的人员
                            String reason,//邀请群加入的申请
                            EMGroupOptions option,//群设置:一定要传
                            MutableLiveData<String> success,
                            MutableLiveData<String> failed
    ) {
        emClient.groupManager().asyncCreateGroup(groupName, desc, allMembers, reason, option,
                new EMValueCallBack<EMGroup>() {
                    @Override
                    public void onSuccess(EMGroup value) {
                        success.postValue("创建群成功");
                    }

                    @Override
                    public void onError(int error, String errorMsg) {
                        failed.postValue(errorMsg);
                    }
                });
    }

    public void findPublicGroups(int pageSize, String cursor, MutableLiveData<List<EMGroupInfo>> success,
                                 MutableLiveData<String> failed) {
        //异步获取所有公开群列表,不需要考虑线程
//        emClient.groupManager().asyncGetPublicGroupsFromServer(pageSize, cursor,
//                new EMValueCallBack<EMCursorResult<EMGroupInfo>>() {
//                    @Override
//                    public void onSuccess(EMCursorResult<EMGroupInfo> value) {
//                        success.postValue(value.getData());
//                    }
//
//                    @Override
//                    public void onError(int error, String errorMsg) {
//                        failed.postValue(errorMsg);
//                    }
//                });
        //同步查找群的方法,直接去文档赋值
        new Thread(() -> {
            EMCursorResult<EMGroupInfo> result = null;
            try {
                result = emClient.groupManager().getPublicGroupsFromServer(pageSize, cursor);
                List<EMGroupInfo> groupsList = result.getData();
                success.postValue(groupsList);
            } catch (HyphenateException e) {
                failed.postValue(e.getMessage());
                throw new RuntimeException(e);
            }
        }).start();

    }

    public void joinGroup(String groupId, MutableLiveData<String> success, MutableLiveData<String> failed) {
        emClient.groupManager().asyncJoinGroup(groupId, new EMCallBack() {
            @Override
            public void onSuccess() {
                success.postValue("加入群成功");
            }

            @Override
            public void onError(int code, String error) {
                failed.postValue(code + "");
            }
        });
    }

    public void findJoinedGroup(MutableLiveData<List<EMGroup>> success, MutableLiveData<String> failed) {
        emClient.groupManager().asyncGetJoinedGroupsFromServer(new EMValueCallBack<List<EMGroup>>() {
            @Override
            public void onSuccess(List<EMGroup> value) {
                success.postValue(value);
            }

            @Override
            public void onError(int error, String errorMsg) {
                failed.postValue(errorMsg);
            }
        });
    }
}
